Grokking Database Fundamentals for Tech Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction to Database Systems
Course Overview
Overview of Databases
Core Components of Database
Database Architectures
Relational Vs. Non-relational Databases
Data Storage in DBMS
Data Storage Fundamentals
File Organization in DBMS
Data Compression and Encoding
Quiz
Indexing in DBMS
Introduction to Database Indexes
Single Level Indexing
Tree-Based Indexing Techniques
Hash-Based Indexing
Bloom Filters
Quiz
Transaction Management
Understanding the Transaction
Atomicity
Consistency
Isolation
Durability
Quiz
Understanding the Concurrency Control
Optimistic vs. Pessimistic Concurrency Control
Lock-Based Concurrency Control
Timestamp-Based Concurrency Control
Multiversion Concurrency Control (MVCC)
Quiz
Fundamentals of Distributed Databases
Partitioning in Databases
Partitioning Types
Sharding in Databases
Distributed Query Processing
Quiz
Replication in Databases
Replication Topologies
Understanding the Consistency Models in Databases
The CAP Theorem and Its Implications
Quiz
Understanding the Consensus
Consensus algorithms
Leader Election Strategies
Quiz
Understanding the Fault Tolerance
Logging Mechanisms in Databases
Checkpointing and Recovery Basics
Coordinated Recovery in Distributed Systems
High Availability Through Replication and Failover
Ensuring Data Resilience: Redundancy, Mirroring, and Recovery
Quiz
Introduction to NoSQL
Key-Value Stores
Document-Oriented Databases
Column-Family and Wide-Column Stores
Graph Databases
Quiz
Quora System Design & Database Design
TikTok System Design & Database Design
Airbnb System Design & Database Design
Snapchat System Design & Database Design
Consistency

What is Consistency?

Consistency, one of the ACID properties, ensures that a database adheres to all predefined rules and constraints at all times. A transaction must transition the database from one valid state to another while maintaining all data integrity constraints.

Key Points:

  • A transaction cannot violate database rules, such as unique keys, foreign key constraints, or business rules.
  • If a transaction fails, the database state must remain unchanged, preserving its integrity.

The database always follows rules, ensuring valid data before and after transactions.

Consistency Explained Through the Example

Let’s analyze the scenario in the provided image:

Image
  1. Initial State (T1):

    • Person A’s Balance: $300
    • Person B’s Balance: $400
    • Total Balance: 400 = $700

    This represents a consistent state where the total balance across accounts is correct.

  2. Transaction Begins (T2):

    • A transaction is initiated to transfer $100 from Person A to Person B.
  3. Step 1: Debit Operation (T3):

    • $100 is debited from Person A’s account.
    • New Balance:
      • Person A: 100 = $200
      • Person B: $400 (unchanged so far)
    • Total Balance: 400 = $600 (Inconsistent state due to intermediate execution).
  4. Step 2: Credit Operation (T4):

    • $100 is credited to Person B’s account.
    • New Balance:
      • Person A: $200
      • Person B: 100 = $500
    • Total Balance: 500 = $700 (Back to a consistent state).
  5. Commit Operation (T5):

    • The transaction is successfully committed, and changes become permanent.
  6. Final State (T6):

    • Person A’s Balance: $200
    • Person B’s Balance: $500
    • Total Balance: 500 = $700

    The database maintains a consistent state after the transaction.

How Consistency is Achieved

  1. Validation Rules:

    • Enforce constraints such as unique keys, foreign keys, and check constraints.
    • Example: Ensure that account balances never go negative during the transaction.
  2. Pre-Transaction Checks:

    • Validate that all conditions required for a successful transaction are met before execution.
    • Example: Check Person A’s account has sufficient funds before debiting.
  3. Post-Transaction Verification:

    • Ensure the database satisfies all integrity constraints after the transaction.
    • Example: Verify that the total balance across all accounts is preserved.
  4. Atomic Execution:

    • Consistency relies on atomicity to prevent partial updates. If any step in the transaction fails, the database reverts to its previous state.

Scenarios Where Consistency Can Be Violated

  1. Constraint Violation:

    • Example: Adding a record with a duplicate primary key.
    • The database will reject the operation and maintain consistency.
  2. System Crashes During Execution:

    • If the system crashes after debiting but before crediting, total balance integrity is lost.
  3. Logical Errors in Business Rules:

    • Example: Transferring an amount larger than the sender’s balance due to a programming bug.
  4. Concurrent Transactions Without Isolation:

    • Example: Two transactions simultaneously debiting from the same account without proper locking.
Atomicity
Isolation
Mark as Completed

On this page

What is Consistency?

Consistency Explained Through the Example

How Consistency is Achieved

Scenarios Where Consistency Can Be Violated